#include "reader_info_add.h"
#include "ui_reader_info_add.h"

reader_Info_add::reader_Info_add(QSqlTableModel *M, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::reader_Info_add)
{
    ui->setupUi(this);
    this->M = M;
    db = DBOperation::getInstance();
    db->open();

    this->setStyleSheet("QDialog{background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(251,102,102, 200), stop:1 rgba(20,196,188, 210));}");
    ui->address_lineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                        "color: rgb(31,31,31);"		//字体颜色
                                        "padding-left:20px;"       //内边距-字体缩进
                                        "background-color: rgb(255, 255, 255);" //背景颜色
                                        "border:2px solid rgb(20,196,188);border-radius:15px;");
    ui->mail_lineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                     "color: rgb(31,31,31);"		//字体颜色
                                     "padding-left:20px;"       //内边距-字体缩进
                                     "background-color: rgb(255, 255, 255);" //背景颜色
                                     "border:2px solid rgb(20,196,188);border-radius:15px;");
    ui->password_lineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                         "color: rgb(31,31,31);"		//字体颜色
                                         "padding-left:20px;"       //内边距-字体缩进
                                         "background-color: rgb(255, 255, 255);" //背景颜色
                                         "border:2px solid rgb(20,196,188);border-radius:15px;");
    ui->realname_lineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                         "color: rgb(31,31,31);"		//字体颜色
                                         "padding-left:20px;"       //内边距-字体缩进
                                         "background-color: rgb(255, 255, 255);" //背景颜色
                                         "border:2px solid rgb(20,196,188);border-radius:15px;");
    ui->telephone_lineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                          "color: rgb(31,31,31);"		//字体颜色
                                          "padding-left:20px;"       //内边距-字体缩进
                                          "background-color: rgb(255, 255, 255);" //背景颜色
                                          "border:2px solid rgb(20,196,188);border-radius:15px;");
    ui->username_lineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                         "color: rgb(31,31,31);"		//字体颜色
                                         "padding-left:20px;"       //内边距-字体缩进
                                         "background-color: rgb(255, 255, 255);" //背景颜色
                                         "border:2px solid rgb(20,196,188);border-radius:15px;");
    ui->reader_Code_lineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                            "color: rgb(31,31,31);"		//字体颜色
                                            "padding-left:20px;"       //内边距-字体缩进
                                            "background-color: rgb(255, 255, 255);" //背景颜色
                                            "border:2px solid rgb(20,196,188);border-radius:15px;");
    ui->add_pushButton->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(20,196,188, 210);"
                "font:36px '宋体';}"
                "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(251,102,102, 200);"
                "color:rgb(255,255,255);}"
                "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");
    ui->cancel_pushButton->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(20,196,188, 210);"
                "font:36px '宋体';}"
                "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(251,102,102, 200);"
                "color:rgb(255,255,255);}"
                "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");
    ui->pushButton->setStyleSheet("QPushButton{background-color: rgb(255,255,255);"
                                  "border: none;border-radius:35px;}"
                                  "QPushButton:hover{background-color: rgb(255,255,255);}"//hover
                                  "QPushButton:pressed{background-color: rgb(255,255,255);}");
}

reader_Info_add::~reader_Info_add()
{
    db->close();
    delete ui;
}


void reader_Info_add::on_add_pushButton_clicked()
{
    QString username = ui->username_lineEdit->text();
    QString password = ui->password_lineEdit->text();
    QString realName = ui->realname_lineEdit->text();
    QString address=ui->address_lineEdit->text();
    QString tel=ui->telephone_lineEdit->text();
    QString email=ui->mail_lineEdit->text();
    QString readerNumber=ui->reader_Code_lineEdit->text();
    QString sex=ui->sex_comboBox->currentText();
    QString birthday=ui->birthday_dateEdit->text();
    QDateTime registerDate = QDateTime::currentDateTime();
    QString sql;
    if (username.isEmpty() || password.isEmpty()||realName.isEmpty()||address.isEmpty()||tel.isEmpty()||email.isEmpty()||readerNumber.isEmpty()) {
        QMessageBox::warning(this, "警告", "请输入必填内容");
    }
    else {
        sql = "select * from reader_info;";
        db->operate(sql);
        bool flag = true;
        while (db->getNext()) {
            if (db->getValue("username") == username) {
                flag = false;
                QMessageBox::warning(this, "提示", "该读者已存在");
                break;
            }
        }
        if (flag) {
            sql = QString("INSERT INTO reader_info VALUES('%0', '%1', '%2', '%3','%4','%5','%6','%7','%8','%9');").arg(username, password, realName,sex,birthday,address,tel,email,registerDate.toString("yyyy-MM-dd hh:mm:ss"),readerNumber);
            db->operate(sql);
            QMessageBox::information(this, "提示", "添加成功");
            on_cancel_pushButton_clicked();
        }
    }
}


void reader_Info_add::on_cancel_pushButton_clicked()
{
    M->select();
    this->close();
}

